import pandas as pd
import pickle
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# 假设data.csv是一个包含有关销售数据的CSV文件
sales_data = pd.read_csv('data/sale.csv')

# 分离特征（预测变量）和目标（响应变量）
X = sales_data[['Year', 'Month', 'Day']]  # 选择相关的日期特征作为预测变量
y = sales_data['Sales']  # 销售额作为目标响应变量

# 划分数据集为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 创建线性回归模型
model = LinearRegression()

# 训练模型
model.fit(X_train, y_train)

# 进行预测
y_pred = model.predict(X_test)

# 评估模型
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")

#保存模型
with open('data/model/model.pkl', 'wb') as file:
    pickle.dump(model, file)
